var table = null;
$(document).ready(function () {
    table = $('#example2').DataTable({
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sSearch": "搜索:",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        },
        "ajax": "/rm/getAllUser",
        "columns": [
            {"data":null},
            {
                "data": "userName"
            },
            {
                "data": "realName"
            },
            {
                "data": "gendar"
            },
            {
                "data": "role"
            },
            {
                "data": "profession"
            },
            {
                "data": "unit"
            },
            {
                "data": "major"
            },
            {
                "data": "tel"
            },
            {"data": null}
        ],
        "order": [
            [1, 'desc']
        ],
        columnDefs: [{
            targets: 9,
            render: function (a, b, c, d) {
                return  '<a href="javascript:;" onclick="delUser('+c.id+')">删除</a> | <a href="personEdit.html?id='+c.id+'">编辑</a>';
            }
        }
        ],
    });
    //为datatable增加序号
    table.on('order.dt search.dt',function(){
        table.column(0,{
            search: 'applied',
            order: 'applied'
        }).nodes().each(function(cell,i){
            i = i + 1;
            var page = table.page.info();
            var pageno = page.page;
            var length = page.length;
            var columnIndex = (i+pageno*length);
            cell.innerHTML = columnIndex;
        });
    }).draw();
});


/**
 * 删除数据
 * @param id
 */
function delUser(id) {
    layer.confirm('确定删除该用户吗？', {
        skin:'myTips',
        btn : ['确定','取消'],
        btn1:function(){
            $.ajax({
                url: "/rm/deleteById",
                type:'post',
                data: {
                    "ids": id
                },
                success: function (data) {
                    table.ajax.reload();
                    layer.msg("删除成功！");
                    console.log("删除成功" + data);
                }
            });
        },
    });

}
